package com.frame.work.frame.deal.device0faa;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.frame.deal.util.WeakCurrentDeviceUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 可燃气体报警器
 */
@Data
public class KeRanQiTiBaoJingQi_0FAA8102 extends BaseFrame {

    private String switchState;

    public KeRanQiTiBaoJingQi_0FAA8102(String gatewayMac, String data) {
        //将基础数据进行处理
        super(gatewayMac, data);
        //配置开关状态
        this.setSwitchState(getAlarmState(data));
        //核心业务处理
        CoreLogicDeal(data);
        //配置Action信息
        getActions(data);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));

    }


    /**
     * @Description:获取报警状态
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/27 10:33
     */
    private String getAlarmState(String data) {
        return StringUtils.substring(data, data.length() - 2, data.length());
    }

    /**
     * @Description:
     * @param: data
     * @return: void
     * @author:wjming
     * @Date:2021/1/27 20:44
     */
    private void getActions(String data) {
        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("reportType", this.getDataType());
        actions.put("alarmState", this.switchState);
        this.setActions(actions);
    }

    /**
     * @Description: 核心逻辑处理
     * @param: data
     * @return: void
     * @author:wjming
     * @Date:2021/1/27 20:47
     */
    public void CoreLogicDeal(String data) {
        String type = "";
        String content = "";
        switch (this.getDataType()) {
            case "00"://触发上报
                break;
            case "80"://主动定时上报(只发送mqtt)
                //报警
                type = StringUtils.equals("01", getAlarmState(data)) ? TypeEnum.WARNING.code() : TypeEnum.STATE.code();
                content = StringUtils.equals("01", getAlarmState(data)) ? "警报" : "解除警报";
                WeakCurrentDeviceUtil.SaveLogAndSendJpush(this.getDeviceMac(), this.getGatewayMac(), content, type);
                break;
        }
    }
}
